<template>
	<view class="content">
		<u-navbar title="消息" :placeholder="true" bgColor="rgba(0,0,0,0.0)" :autoBack="false">
			<view slot="left"></view>
		</u-navbar>
		<u-toast ref="uToast" />
		<view class="fun-box" @click="gofriend">
			<image src="https://m.lqvvvv.com/assets/static/message/icon1.png" mode=""></image>
			<view class="right-box">
				<view class="title">好友列表</view>
				<view class="mintitle">
					我的好友列表
				</view>
			</view>
		</view>
		<view v-for="(item, index) in sessionList" :key="index" @click="goToSessionInfo(item)">
			<session :item="item"></session>
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<!-- <u-tabbar :value="2" :fixed="true" :placeholder="true" activeColor="#5755F2" inactiveColor="#2E314A"
			:safeAreaInsetBottom="true">
			<u-tabbar-item text="首页" v-if="tabshow == 0" @click="gotaburl('首页')">
				<image style="width: 38rpx; height: 38rpx;" slot="active-icon" src="/static/tabbar/home1.png"></image>
				<image style="width: 38rpx; height: 38rpx;" slot="inactive-icon" src="/static/tabbar/home0.png"></image>
			</u-tabbar-item>
			<u-tabbar-item text="商家" @click="gotaburl('商家')">
				<image style="width: 38rpx; height: 38rpx;" slot="active-icon" src="/static/tabbar/business1.png">
				</image>
				<image style="width: 38rpx; height: 38rpx;" slot="inactive-icon" src="/static/tabbar/business0.png">
				</image>
			</u-tabbar-item>
			<u-tabbar-item text="消息" @click="gotaburl('消息')">
				<image style="width: 38rpx; height: 38rpx;" slot="active-icon" src="/static/tabbar/msg1.png"></image>
				<image style="width: 38rpx; height: 38rpx;" slot="inactive-icon" src="/static/tabbar/msg0.png"></image>
			</u-tabbar-item>
			<u-tabbar-item text="我的" @click="gotaburl('我的')">
				<image style="width: 38rpx; height: 38rpx;" slot="active-icon" src="/static/tabbar/my1.png"></image>
				<image style="width: 38rpx; height: 38rpx;" slot="inactive-icon" src="/static/tabbar/my0.png"></image>
			</u-tabbar-item>
		</u-tabbar> -->
		<!-- #endif -->
	</view>
</template>

<script>
	import session from "@/components/session/session.vue"
	export default {
		components: {
			session
		},
		data() {
			return {
				pageDataLoadBool: false,
				sessionList: [],
				tabshow: 0
			}
		},
		onLoad() {
			this.ws.pageFun(this.pageDataLoad, this);
			setTimeout(() => {
				console.log(this.sessionList)
			}, 1000)
		},
		onShow: function() {
			// #ifdef MP-WEIXIN
			this.showinfo()
			// #endif
			var that = this
			if (this.ws.pageRefresh.message) {
				// 被其他页面通知刷新会话列表(更新列表中的好友备注等)
				this.ws.pageFun(this.pageDataLoad, this);
				this.ws.clearPageRefresh()
				this.ws.pageRefresh.message = false
				this.ws.pageRefresh.sessionInfo = false
				this.ws.messageShow = [] // 已经重载,无需执行messageShow中的方法
				return;
			}
			if (this.ws.messageShow.length) {
				for (let m in this.ws.messageShow) {
					if (typeof this.ws.messageShow[m] == 'function') {
						this.ws.messageShow[m](that)
						this.ws.messageShow[m] = null
					}
				}
			}
			if (!this.pageDataLoadBool) {
				return;
			}
			that.ws.checkNetwork(that)
			that.ws.pageFun(function() {
				that.ws.send({
					c: 'ImBase',
					a: 'loadTODO',
					data: {
						'method': 'get-all-count'
					}
				})
			}, that);
		},
		onPullDownRefresh() {
			this.ws.pageFun(this.pageDataLoad(true), this);
			this.ws.onMessageCallBack.set('load_session_list', (msg) => {
				if (msg.data.data.refresh && msg.data.data.method == 'load') {
					this.$refs.uToast.show({
						message: '刷新成功~',
						type: 'success'
					})
					uni.stopPullDownRefresh()
				}
			})
		},
		methods: {
			gotaburl(name) {
				if (name == '首页') {
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
				if (name == '商家') {
					uni.switchTab({
						url: '/pages/business/index'
					})
				}
				if (name == '消息') {
					uni.switchTab({
						url: '/pages/message/index'
					})
				}
				if (name == '我的') {
					uni.switchTab({
						url: '/pages/my/index'
					})
				}
			},
			showinfo() {
			let params = {
				// #ifdef APP
				name: 'hide_status',
				// #endif
				// #ifdef MP-WEIXIN
				name: 'miniapp_hide_status'
				// #endif
			}
				this.$Request({
					method: 'POST',
					url: '/api/index/getConfig',
					data: params,
					ismsg: false,
				}).then(res => {
					if (res.code == 1) {
						this.tabshow = res.data
					}
				})
			},
			gofriend() {
				let token = uni.getStorageSync("token") || '';
				if (token != '') {
					this.$navTo.togo('/pages/message/friend/index')
				} else {
					uni.showModal({
						title: '提示',
						content: '请登录！体验完整功能！',
						confirmText: '去登录',
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
								uni.reLaunch({
									url: '/pages/login/index'
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			goToSessionInfo(item) {
				console.log(item)
				if (item.nickname == "旅趣官方客服") {
					wx.openCustomerServiceChat({
						extInfo: {
							url: 'https://work.weixin.qq.com/kfid/kfcb8f13051fb4e767f' //客服地址链接
						},
						corpId: 'ww3d05dfba76a348f3', //必须和你小程序上的一致
						success(res) {
							console.log(res, 1)
						},
						fail(res) { 
							console.log(res, 2)
						},
					})
				} else {
					this.$navTo.togo('/pages/session-info/session-info', {
						id: item.id
					})
				}
			},
			pageDataLoad: function(refresh = false) {
				this.pageDataLoadBool = true
				this.ws.send({
					c: 'User',
					a: 'loadSessionList',
					data: {
						'refresh': refresh
					}
				})
				this.ws.send({
					c: 'ImBase',
					a: 'loadTODO',
					data: {
						'method': 'get-all-count'
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.content {
		min-height: 100vh;
		background-image: url('https://m.lqvvvv.com/assets/static/index/nav-bg.png');
		background-repeat: no-repeat;
		background-size: 100% 800rpx;
		padding: 30rpx;

		.fun-box {
			display: flex;
			align-items: center;
			padding: 24rpx 0;

			image {
				width: 100rpx;
				height: 100rpx;
			}

			.right-box {
				margin-left: 15rpx;

				.title {
					font-weight: 500;
					font-size: 32rpx;
					color: #2D2D2D;
				}

				.mintitle {
					font-weight: 400;
					font-size: 28rpx;
					color: #9F9F9F;
					margin-top: 10rpx;
				}
			}
		}
	}
</style>